package com.acwing.lc.part1;

/**
 * @author rkc
 * @date 2022/9/12 16:02
 */
public class LC38外观数列 {

    public String countAndSay(int n) {
        String ans = "1";
        for (int i = 1; i < n; i++) ans = desc(ans);
        return ans;
    }

    private String desc(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); ) {
            int j = i + 1;
            while (j < s.length() && s.charAt(j) == s.charAt(i)) j++;
            sb.append(j - i).append(s.charAt(i));
            i = j;
        }
        return sb.toString();
    }
}
